package com.info.threads;

import java.util.Collection;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.net.operation.SendThread;
import org.net.operation.TerServer;

import com.info.server.Event;
import com.info.server.StartServer;
import com.info.utils.InfoPacket;

public class PutInfoThread extends Thread {

	private static Logger logger = Logger.getLogger(PutInfoThread.class);
	boolean runflag = false;
	private SendThread sendThread = null;

	public PutInfoThread() {
		this.sendThread = new SendThread();
		this.sendThread.start();
	}

	public void run() {
		while (!this.runflag) {
			Event msg = new Event();
			StringBuffer result = new StringBuffer("");
			try {
				if (TerServer.channelHash != null) {
					Collection s = TerServer.channelHash.keySet();
					Iterator iter = s.iterator();
					while(iter.hasNext()) {
						String terID = (String) iter.next();
						if (terID != null) {
							if (GetPutInfoThread.hm.containsKey(terID)) {
								Event putMsg = (Event) GetPutInfoThread.hm
										.get(terID);
								if (!putMsg.getProNum().equals("81")
										& !putMsg.getProNum().equals("82")
										& !putMsg.getProNum().equals("84")
										& !putMsg.getProNum().equals("85")
										& !putMsg.getProNum().equals("86")
										& !putMsg.getProNum().equals("41")
										& !putMsg.getProNum().equals("42")) {
									if (putMsg.getProNum().equals("83")) {
										result.append(
												StartServer.MSG_START_FLAG)
												.append(putMsg.getProNum())
												.append(putMsg.getTerID())
												.append(putMsg.getSerNum())
												.append(putMsg.getInfoLength())
												.append(putMsg.getTargetCode())
												.append(putMsg.getLatitude())
												.append(putMsg.getLongitude())
												.append(putMsg.getStatusBits())
												.append(StartServer.MSG_END_FLAG);
										GetPutInfoThread.hm.remove(terID);
									} else if (putMsg.getProNum().equals("43")) {
										result.append(
												StartServer.MSG_START_FLAG)
												.append(putMsg.getProNum())
												.append(putMsg.getReservation())
												.append(putMsg.getTerID())
												.append(putMsg.getContents0x())
												.append(StartServer.MSG_END_FLAG);
										GetPutInfoThread.hm.remove(terID);
									} else {
										result.append(
												StartServer.MSG_START_FLAG)
												.append(putMsg.getProNum())
												.append(putMsg.getTerID())
												.append(putMsg.getDirection())
												.append(putMsg.getInfoLength())
												.append(putMsg.getContents())
												.append(StartServer.MSG_END_FLAG);
									}
								}
								Thread.sleep(1000);
								msg.setMsgInfo(result.toString());
								msg.setId(terID);
								msg.setUppack((InfoPacket)TerServer.channelHash.get(terID));
								GetPutInfoThread.hm.remove(terID);
								logger.info("SHW#下发终端 :"  +terID +"    "+ result);
								this.sendThread.addMsg(msg);
							}
						}
					}
				}
				
			} catch (Exception e1) {
				e1.printStackTrace();
			}

		}
	}
}
